package cn.itsource.article.controller;

import cn.itsource.article.domain.Comment;
import cn.itsource.article.service.ICommentService;
import cn.itsource.system.annotation.PermissionAnnotation;
import cn.itsource.util.AjaxResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/comment")
public class CommentController {
    @Autowired
    private ICommentService service;

    @PostMapping
    //添加上这个注解过后 就表示为特定资源 且这个注解设置的值会直接显示在数据库上 url可写可不写
   /* @PermissionAnnotation(name = "傻逼",url = "aaaaadw",sn ="cccc",descs = "我好烦")*/
    public AjaxResult save(@RequestBody Comment content){
        try {
            service.save(content);
            return AjaxResult.me().setSuccess(true).setMessage("操作成功");
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("操作失败");
        }
    }

    @PatchMapping("/{articleId}")
    /*@PermissionAnnotation(name = "傻8",sn ="5828",descs = "我好烦")*/
    public AjaxResult aboutArticles(@PathVariable("articleId")  Long articleId){
        try {
            return AjaxResult.me().setResultObj(service.finByArticleId(articleId));
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("查询失败");
        }
    }

    @DeleteMapping("{id}")
    public AjaxResult delete(@PathVariable("id") String id){
        try {
            service.deleteByPrimaryKey(id);
            return AjaxResult.me().setSuccess(true).setMessage("操作成功");
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("删除失败");
        }
    }
}
